using Domain.Entities;
using Domain.Misc;
using Domain.Services;
using System;
using UserInterface.Misc;

namespace UserInterface.Admin
{
    public partial class EmailSettings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Domain.Entities.Settings settings = BoardSettingsManager.Instance.Settings;

                FromAddressTextBox.Text = settings.EmailFromAddress;
                FromDisplayNameTextBox.Text = settings.EmailFromDisplayName;
                SmtpHostTextBox.Text = settings.EmailSmtpHost;
                SmtpPortTextBox.Text = settings.EmailSmtpPort.ToString();
                SmtpUsernameTextBox.Text = settings.EmailSmtpUsername;
                SmtpPasswordTextBox.Text = settings.EmailSmtpPassword;
                EnableSslRadioButtonList.SelectedValue = settings.EmailEnableSsl.ToString();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            string fromAddress = FromAddressTextBox.Text.Trim();
            string fromDisplayName = FromDisplayNameTextBox.Text.Trim();
            string smtpHost = SmtpHostTextBox.Text.Trim();
            int smtpPort = Convert.ToInt32(SmtpPortTextBox.Text);
            string smtpUsername = SmtpUsernameTextBox.Text.Trim();
            string smtpPassword = SmtpPasswordTextBox.Text.Trim();
            bool enableSsl = Convert.ToBoolean(EnableSslRadioButtonList.SelectedValue);

            SettingsService.UpdateEmailSettings(fromAddress, fromDisplayName, smtpHost, smtpPort, smtpUsername, smtpPassword, enableSsl);
        }
    }
}
